import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/model/review_model.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';

///审核详情页通用指导器
class ReviewPresenter {
  ///body视图状态
  StateSetter? setStateBody;

  ///详情数据
  AppHttpResult<String, String>? mDatas = null;

  ///历史详情数据
  AppHttpResult<String, String>? mOldDatas = null;

  ///判断数据是否正在加载
  bool isLoad = true;

  ///获取审核详情数据
  getContractapplygetinfo(BuildContext context,
      {ResponseCallbackWithVoid? callback = null}) {
    isLoad = true;
    //获取详情数据
    ReviewModel.getContractapplygetinfo(context, true, History: false,
        onError: (code, error) {
      isLoad = false;
      setStateBody?.call(() {});
      AppDialog.showDialogWithError(context, code, error)?.then((value) {
        AppRouteHelper.pop(context); //网络异常，关闭当前页面
      });
    })?.then((value) {
      isLoad = false;
      if (value.isSuccessForCode()) {
        try {
          mDatas = value;
          setStateBody?.call(() {});
          callback?.call();
        } catch (e) {}
      } else {
        //异常
        AppDialog.showDialogWithInfo(context, content: value.MsgDetail)
            ?.then((value) {
          AppRouteHelper.pop(context); //关闭当前页面
        });
      }
    });
    // 获取详情历史数据
    ReviewModel.getContractapplygetinfo(context, false, History: true)
        ?.then((value) {
      try {
        mOldDatas = value;
        setStateBody?.call(() {});
      } catch (e) {}
    });
  }

  ///获取审核详情数据[延迟获取，等待页面加载完成]
  getContractapplygetinfoWithDelay(BuildContext context,
      {ResponseCallbackWithVoid? callback = null}) {
    Future.delayed(Duration(milliseconds: 200)).then((e) {
      getContractapplygetinfo(context, callback: callback); //获取审核详情数据
    });
  }
}
